#include <unistd.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <string.h>  
#include <fcntl.h>  
#include <limits.h>  
#include <sys/types.h>  
#include <sys/stat.h> 

#define FIFO_NAME "my_fifo"
#define TEN_MSG (1024 * 1024 * 10)
#define BUFFER_SIZE PIPE_BUF + 1

int main()
{
    int     fd;
    int     res;
    int     open_mode = O_WRONLY;
    int     bytes_send = 0;
    char    buffer[PIPE_BUF + 1];

    printf("Productor Program beginning...\n");

    if (access(FIFO_NAME, F_OK) == -1)
    {
        res = mkfifo(FIFO_NAME, 0777);
        if (res != 0)
        {
            fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
            exit(-1);
        }
    }

    fd = open(FIFO_NAME, open_mode);
    printf("Process %d result %d\n", getpid(), fd);

    if (fd != -1)
    {
        while (bytes_send < TEN_MSG)
        {
            res = write(fd, buffer, BUFFER_SIZE);
            if (res == -1)
            {
                fprintf(stderr, "Write error on pipe\n");
                exit(-1);
            }
            bytes_send += res;
        }
        close(fd);
    }
    else
    {
        exit(-1);
    }
    unlink(FIFO_NAME);

    printf("Process %d finished, %d bytes send\n", getpid(), bytes_send);
    exit(0);
}
